import { CloseOutlined, FolderOutlined, HomeOutlined } from '@ant-design/icons';
import { Button, Space, Tree } from 'antd';

import ProCard from '@ant-design/pro-card';
import { DataNode } from 'antd/lib/tree';
import React, { useEffect, useState } from 'react';
import { treeOrganization } from '@/services/api/api-datahub-keyword';
import { useAccess } from 'umi';


interface TreeOrganizationProps {
    onSelect: (info: any) => void;
    onRefurbish: () => void;
}
const TreeOrganization: React.FC<TreeOrganizationProps> = ({ onSelect, onRefurbish }) => {
    /**
     * 初始化树形数据
     */
    const [dataTree, setDataTreeState] = useState<DataNode[]>();

    /**
     * 树形列表选择数据
     */
    const [selectNode, setSelectNodeState] = useState<any>();
    const access = useAccess();
    /**
     * 预备Tree结构数据
     * @param data
     */
    function readyTreeData(data: AdminOrganizationData.Organization[]): any {
        const treeData = new Array();
        if (data) {
            data.forEach((val) => {
                treeData.push({
                    title: val.name,
                    key: val.id,
                    item: val,
                    children: readyTreeData(val.children as AdminOrganizationData.Organization[]),
                    icon:
                        val.state === 1 ? (
                            val.isFolder === 0 ? (
                                <HomeOutlined />
                            ) : (
                                <FolderOutlined />
                            )
                        ) : (
                            <CloseOutlined />
                        ),
                });
            });
        }
        return treeData;
    }

    /**
     * 加载权限详情
     * @param key
     */
    const loadOrganizationData = async () => {
        try {
            const response = await treeOrganization();
            if (response.code === 0) {
                const treeData = new Array<DataNode>();
                treeData.push({
                    title: '重庆市新闻选题管理平台',
                    key: '0',
                    item: { id: '0', name: '重庆市新闻选题管理平台' },
                    children: readyTreeData(response.data),
                });
                setDataTreeState(treeData);
                onSelect({ node: treeData[0] });
            }
        } catch (error) { }
    };

    /**
     * 恢复默认
     */
    useEffect(() => {
        loadOrganizationData();
    }, []);

    return (
        <ProCard
            title="单位树"
        >
            {dataTree && dataTree.length ? (
                <Tree
                    defaultExpandAll
                    height={800}
                    showLine={{ showLeafIcon: false }}
                    showIcon={true}
                    treeData={dataTree}
                    defaultSelectedKeys={[dataTree[0].key]}
                    onSelect={(selectedKeys: React.Key[], info: any) => {
                        console.log('=========', info);
                        onSelect(info);
                        setSelectNodeState(info);
                    }}
                />
            ) : (
                '暂无数据'
            )}
        </ProCard>
    );
};
export default TreeOrganization;
